package cn.edu.zut.soft.student.service.teacher.impl;

import cn.edu.zut.soft.student.bean.PageBean;
import cn.edu.zut.soft.student.dao.TeachCourseMapper;
import cn.edu.zut.soft.student.dao.TeacherMapper;
import cn.edu.zut.soft.student.model.*;
import cn.edu.zut.soft.student.model.enums.Role;
import cn.edu.zut.soft.student.service.teacher.TeacherBo;
import cn.edu.zut.soft.student.service.teacher.vo.TeacherQuery;
import cn.edu.zut.soft.student.utils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author ChrisPhantom
 * @CreatDate 2021/02/22 21:47
 */

@Service
public class TeacherBoImpl implements TeacherBo {
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private TeachCourseMapper teachCourseMapper;

    @Override
    public int addTeacher(Teacher teacher) {
        TeachCourseKey teachCourseKey = new TeachCourseKey();
        teachCourseKey.setTeachclasscourseid(teacher.getTeachcoursename());
        teachCourseKey.setTeachclassteacherid(teacher.getTeacherid());
        teacherMapper.insert(teacher);
        return teachCourseMapper.insert(teachCourseKey);
    }

    @Override
    public int importTeachers(MultipartFile file) throws Exception {
        List<Teacher> teachers = ExcelUtil.getTeachersFromExcel(file.getInputStream());
        teachers.forEach(teacher -> addTeacher(teacher));
        return 0;
    }

    @Override
    public int deleteTeacher(String teacherid) {
        TeachCourseExample teachCourseExample = new TeachCourseExample();
        TeachCourseExample.Criteria criteria = teachCourseExample.createCriteria();
        criteria.andTeachclassteacheridEqualTo(teacherid);
        teachCourseMapper.deleteByExample(teachCourseExample);
        return teacherMapper.deleteByPrimaryKey(teacherid);
    }

    @Override
    public int updateTeacher(Teacher teacher) {
        return teacherMapper.updateByPrimaryKey(teacher);
    }

    @Override
    public Teacher queryTeacherById(String teacherid) {
        return teacherMapper.selectByPrimaryKey(teacherid);
    }

    @Override
    public PageBean<List<Teacher>> queryTeachers(TeacherQuery teacherQuery) {
        TeacherExample teacherExample = new TeacherExample();
        teacherExample.setOffset(teacherQuery.getOffset());
        teacherExample.setLimit(teacherQuery.getLimit());
        TeacherExample.Criteria criteria = teacherExample.createCriteria();
        if (teacherQuery.getTeacherid() != null && !"-1".equals(teacherQuery.getTeacherid())) {
            criteria.andTeacheridLike("%" + teacherQuery.getTeacherid() + "%");
        }
        if (teacherQuery.getTeachername() != null && !"-1".equals(teacherQuery.getTeachername())) {
            criteria.andTeachernameLike("%" + teacherQuery.getTeachername() + "%");
        }
        List<Teacher> teachers = teacherMapper.selectByExample(teacherExample);
        teachers.forEach(teacher -> teacher.setTeachcoursename(teachCourseMapper.queryTeachCourse(teacher.getTeacherid()).getCoursename()));
        return new PageBean<>(teachers, teacherMapper.countByExample(teacherExample));
    }

    @Override
    public int resetPassword(String teacherid, String password) {
        return teacherMapper.updatePassword(password, teacherid);
    }

    @Override
    public Course queryTeachCourse(String teacherid) {
        return teachCourseMapper.queryTeachCourse(teacherid);
    }
}
